﻿using PLCConsole.PLCSerStation;
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
using System.Windows.Forms.VisualStyles;

namespace PLCConsole
{
    public partial class FPLCInfo_WFDRVX : Form
    {
        protected Station_WFDRVX tStationWFDRVX { get; private set; } = null;
        private DateTime LastSetPort { get; set; } = DateTime.Now;

        public FPLCInfo_WFDRVX(Station_WFDRVX SourStation)
        {
            InitializeComponent();
            tStationWFDRVX= SourStation;
        }

        private void aGaugeY_Click(object sender, EventArgs e)
        {
            AGauge btn = (AGauge)sender;
            if (btn.Name.Equals("aGaugeY1"))
            {
                tStationWFDRVX.Set_YPort(0, aGaugeY1.Value == 0);
            }
            if (btn.Name.Equals("aGaugeY2"))
            {
                tStationWFDRVX.Set_YPort(1, aGaugeY2.Value == 0);
            }
            if (btn.Name.Equals("aGaugeRunType"))
            {
                aGaugeRunType.Value += 180;
                if (aGaugeRunType.Value >= 360)
                    aGaugeRunType.Value = 0;
                switch (aGaugeRunType.Value)
                {
                    case 0:
                        tStationWFDRVX.RunType = 0;
                        break;
                    case 180:
                        tStationWFDRVX.RunType = 1;
                        break;
                }
            }
        }

        private void tDeamon_Tick(object sender, EventArgs e)
        {
            if (tStationWFDRVX == null)
            {
                textBoxStaID.Text = "";
                labelLastAction.Text = "";
                aGaugeMotor.Value = 0;
                aGaugeMotor.MaxValue = 1;
                aGaugeMotor.MinValue = 0;
                aGaugeMotor.GaugeLabels.FindByName("GaugeLabelMotorStatus").Text = "";
                aGaugeMotor.GaugeLabels.FindByName("GaugeLabelTick").Text = "";
            }
            if ((tStationWFDRVX != null) && tStationWFDRVX.HasFlashRegs)
            {
                Text = "WFDRVX[" + tStationWFDRVX.StaID + "]";
                textBoxStaID.Text = tStationWFDRVX.StaID;

                aGaugeX1.Value = tStationWFDRVX.getXValue(0) ? 1 : 0;
                aGaugeX2.Value = tStationWFDRVX.getXValue(1) ? 1 : 0;
                labelAD1.Text = string.Format("{0}mV", tStationWFDRVX.AD1.ToString());
                labelAD2.Text = string.Format("{0}mV", tStationWFDRVX.AD1.ToString());
                lbDigitalMeterTemp.Value = tStationWFDRVX.Tempture;
                lbDigitalMeterRH.Value = tStationWFDRVX.Humidity;
                if ((DateTime.Now - LastSetPort).TotalMilliseconds > 500)
                {
                    aGaugeY1.Value = tStationWFDRVX.getYValue(0) ? 1 : 0;
                    aGaugeY2.Value = tStationWFDRVX.getYValue(1) ? 1 : 0;
                    aGaugeY1.NeedleColor2 = (aGaugeY1.Value == 0) ? Color.Red : Color.Green;
                    aGaugeY2.NeedleColor2 = (aGaugeY2.Value == 0) ? Color.Red : Color.Green;

                }
                labelLastAction.Text = tStationWFDRVX.LastActiveTime.ToString("HH:mm:ss.fff");
                switch (tStationWFDRVX.RunType)
                {
                    case 1:
                        aGaugeRunType.Value = 180;
                        break;
                    case 0:
                    default:
                        aGaugeRunType.Value = 0;
                        break;
                }

                if (tStationWFDRVX.RunType == 1)
                {
                    aGaugeMotor.Value = 0;
                    aGaugeMotor.MinValue = 0;
                    if (tStationWFDRVX.MotorPowerOnTick > 0)
                    {
                        aGaugeMotor.MaxValue = tStationWFDRVX.MotorPowerOnLong;
                        aGaugeMotor.ScaleLinesMajorStepValue = (int)(aGaugeMotor.MaxValue / 10);
                        aGaugeMotor.Value = tStationWFDRVX.MotorPowerOnTick;
                        aGaugeMotor.NeedleColor2 = Color.Green;
                        aGaugeMotor.GaugeLabels.FindByName("GaugeLabelMotorStatus").Text = "Motor Running";
                        aGaugeMotor.GaugeLabels.FindByName("GaugeLabelTick").Text = tStationWFDRVX.MotorPowerOnTick.ToString() + "s";
                        aGaugeMotor.GaugeLabels.FindByName("GaugeLabelTick").Color = Color.Green;
                    }
                    else
                    {
                        aGaugeMotor.MaxValue = tStationWFDRVX.MotorSleepLong;
                        aGaugeMotor.ScaleLinesMajorStepValue = (int)(aGaugeMotor.MaxValue / 10);
                        aGaugeMotor.Value = tStationWFDRVX.MotorSleepping;
                        aGaugeMotor.NeedleColor2 = Color.Red;
                        aGaugeMotor.GaugeLabels.FindByName("GaugeLabelMotorStatus").Text = "Motor Sleep";
                        aGaugeMotor.GaugeLabels.FindByName("GaugeLabelTick").Text = tStationWFDRVX.MotorSleepping.ToString() + "s";
                        aGaugeMotor.GaugeLabels.FindByName("GaugeLabelTick").Color = Color.Red;
                    }
                }
            }
            aGaugeX1.NeedleColor2 = (aGaugeX1.Value == 0) ? Color.Red : Color.Green;
            aGaugeX2.NeedleColor2 = (aGaugeX2.Value == 0) ? Color.Red : Color.Green;
        }
    }
}
